package com.kasax.imgloader.v1;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.kasax.imgloader.v1.cache.Cache;
import com.kasax.imgloader.v1.cache.CacheMode;
import com.kasax.imgloader.v1.cache.DiskCache;
import com.kasax.imgloader.v1.cache.ImageCache;
import com.kasax.imgloader.v1.utils.FileUtils;
import com.kasax.imgloader.v1.utils.LogUtils;
import com.kasax.imgloader.v1.utils.NetworkDispatcher;
import com.kasax.imgloader.v1.utils.Utils;

public class ImgLoader {
    private static volatile ImgLoader instance;
    private ImageLoaderConfiguration appConfig;

    private Cache cache;

    private ImgLoader() {

    }

    public static ImgLoader getInstance() {
        if (instance == null) {
            synchronized (ImgLoader.class) {
                if (instance == null) {
                    instance = new ImgLoader();
                }
            }
        }
        return instance;
    }

    public void displayImage(String url, ImageView imageView) {
        displayImage(url, imageView, null);
    }

    public void displayImage(String url, ImageView imageView, DisplayImageOptions options) {
        if (options != null) {
            Utils.handleScaleType(imageView, options.scaleType());
        }
        imageView.setTag(url);
        Bitmap bitmap = cache.get(url);
        if (bitmap != null) {
            LogUtils.i(getClass(), "走Lrucache");
            imageView.setImageBitmap(bitmap);
            return;
        }

        LogUtils.i(getClass(), "走网络请求");
        NetworkDispatcher.getInstance().loadImage(url, new NetworkDispatcher.Callback() {
            @Override
            public void onSuccess(Bitmap bitmap) {
                LogUtils.i(getClass(), "onSuccess bitmap: " + bitmap);
                if (imageView.getTag() != null && imageView.getTag().equals(url)) {
                    imageView.setImageBitmap(bitmap);
                }
                cache.put(url, bitmap);
            }

            @Override
            public void onFailure(Exception exception) {
                LogUtils.e(getClass(), exception);
            }
        });
    }


    public static final class ImageLoaderConfiguration {
        private final Context context;
        private CacheMode cacheMode;

        public ImageLoaderConfiguration(Builder builder) {
            this.context = builder.context;
            this.cacheMode = builder.cacheMode;
        }

        public static class Builder {
            private final Context context;

            private CacheMode cacheMode;

            public Builder(Context context) {
                this.context = context.getApplicationContext();
            }

            public Builder cacheMode(CacheMode cacheMode) {
                this.cacheMode = cacheMode;
                return this;
            }

            public ImageLoaderConfiguration build() {
                return new ImageLoaderConfiguration(this);
            }
        }
    }

    public void init(ImageLoaderConfiguration configuration) {
        this.appConfig = configuration;

        switch (appConfig.cacheMode) {
            case DISK_CACHE:
                cache = new DiskCache(FileUtils.getSafeSdcardPath("/mnt/sdcard/image_test"));
                break;
            case DOUBLE_CACHE:
                break;
            case MEMORY_CACHE:
                cache = new ImageCache();
                break;
        }
    }
}
